﻿using ASmile.DBModel;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ASmile.Win.ADSearch
{
    internal partial class U_ASRow : UserControl
    {
        public U_ASRow()
        {
            InitializeComponent();
            InitiCompareSign();
            coBoxConnectSign.SelectedIndex = 0;
            coBoxField.ValueMember = "ColumnName";
            coBoxField.DisplayMember = "ColumnTitle";
        }
        class CompareSignModel
        {
            public string CompareSignHZ { set; get; }
            //public CompareSignEnum CompareSign { set; get; }
            public string CompareSign { set; get; }
        }
        /// <summary>
        /// 初始化比较符
        /// </summary>
        void InitiCompareSign()
        {
            var cs = new List<CompareSignModel>();
            cs.Add(new CompareSignModel() { CompareSign = "=", CompareSignHZ = "等于" });
            cs.Add(new CompareSignModel() { CompareSign = "Like", CompareSignHZ = "类似于" });
            cs.Add(new CompareSignModel() { CompareSign = "<>", CompareSignHZ = "不等于" });
            cs.Add(new CompareSignModel() { CompareSign = "not like", CompareSignHZ = "非类似于" });
            cs.Add(new CompareSignModel() { CompareSign = ">", CompareSignHZ = "大于" });
            cs.Add(new CompareSignModel() { CompareSign = ">=", CompareSignHZ = "大于等于" });
            cs.Add(new CompareSignModel() { CompareSign = "<", CompareSignHZ = "小于" });
            cs.Add(new CompareSignModel() { CompareSign = "<=", CompareSignHZ = "小于等于" });
            coBoxCompareSign.DataSource = cs;
            coBoxCompareSign.DisplayMember = "CompareSignHZ";
            coBoxCompareSign.ValueMember = "CompareSign";
        }

        bool _IsFirstRow = false;
        /// <summary>
        /// 是否首行,首行会隐藏连接符和删除按钮
        /// </summary>
        public bool IsFirstRow
        {
            get { return _IsFirstRow; }
            set
            {
                _IsFirstRow = value;
                if (IsFirstRow)
                {
                    coBoxConnectSign.Visible = false;
                }
                else
                {
                    coBoxConnectSign.Visible = true;
                    btnDelete.Visible = true;
                }
            }
        }

        public void InitiColumns(List<Sys_GridColumn> cm)
        {
            coBoxField.DataSource = cm;
        }

        /// <summary>
        /// 设置搜索内容
        /// </summary>
        public void SetDetail(Sys_ADRecord_Detail record)
        {
            coBoxCompareSign.SelectedValue = record.CompareSignStr;

            coBoxConnectSign.Text = record.ConnectorSignStr;

            coBoxField.SelectedValue = record.FieldName;

            txtValue.Text = record.SearchValue;
        }
        /// <summary>
        /// 获取明细
        /// </summary>
        public Sys_ADRecord_Detail GetDetail()
        {
            if (coBoxField.SelectedValue == null) return null;
            Sys_ADRecord_Detail sr = new Sys_ADRecord_Detail();
            sr.CompareSignStr = coBoxCompareSign.SelectedValue.ToString();
            if (coBoxConnectSign.Visible)
            {
                sr.ConnectorSignStr = coBoxConnectSign.Text;
            }
            else
            {
                sr.ConnectorSignStr = string.Empty;
            }
            sr.FieldName = coBoxField.SelectedValue.ToString();
            var column = coBoxField.SelectedItem as Sys_GridColumn;
            sr.DataTypeStr = column.DataType;
            sr.SearchValue = txtValue.Text;
            return sr;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}